package com.hutianao.gqgl.department.control;

import com.hutianao.gqgl.department.entity.SearchDepartmentEntity;
import com.hutianao.gqgl.department.service.DepartmentService;
import com.hutianao.gqgl.job.entity.SearchJobEntity;
import com.hutianao.gqgl.mybatis.entity.LogEntity;
import com.hutianao.gqgl.mybatis.entity.PtDepartmentEntity;
import com.hutianao.gqgl.mybatis.entity.PtJobEntity;
import com.hutianao.gqgl.mybatis.entity.PtUserEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;
import com.hutianao.gqgl.mybatis.ztree.ZtreeEntity;
import com.hutianao.gqgl.spring.control.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController{
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private DepartmentService departService;


    @RequestMapping("/page")
    public String departPage(){
        return "department/departmentPage";
    }


    @RequestMapping("/queryLeftTree")
    @ResponseBody
    public List<ZtreeEntity> queryLeftTree(){
        return departService.queryLeftTree();
    }

    @RequestMapping("/unboundParentIdByDepUuid")
    @ResponseBody
    public String unboundParentIdByDepUuid(@RequestParam("depUuid") String depUuid) {
        logger.info("depUuid:" + depUuid);
        departService.unboundParentIdByDepUuid(depUuid);
        return jsonSuccess("menu.unbound.parent.success");

    }

    @RequestMapping("/queryBoundDepartTree")
    @ResponseBody
    public List<ZtreeEntity> queryBoundDepartTree(@RequestParam("depUuid") String depUuid) {
        logger.info("depUuid:" + depUuid);
        List<ZtreeEntity> list = departService.queryBoundDepartTree(depUuid);
        return list;
    }

    @RequestMapping("/queryDepartTree")
    @ResponseBody
    public  List<ZtreeEntity> queryDepartTree (@RequestParam(value="depUuid",required = false) String depUuid){
        logger.info("depUuid:" + depUuid);
        List<ZtreeEntity> list = departService.queryDepartTree(depUuid);
        return list;
    }

    @RequestMapping("/saveParentIdByDepUuid")
    @ResponseBody
    public String saveParentIdByDepUuid(@RequestParam("depUuid") String depUuid, @RequestParam("belongCenter") String belongCenter) {
        logger.info("depUuid:" + depUuid + " belongCenter:" + belongCenter);
        departService.saveParentIdByDepUuid(depUuid, belongCenter);
        return jsonSuccess("depart.bound.parent.success");

    }

    @RequestMapping("/deleteDepartInfoByIds")
    @ResponseBody
    public String deleteDepartInfoByIds(@RequestParam("depUuid") String depUuid) {
        logger.info("depUuid:" + depUuid );
        boolean b = departService.deleteDepartInfoByIds(depUuid);
        String str = b?"depart.del.depart.success":"depart.del.depart.fail";
        return jsonSuccess(str);
    }

    @RequestMapping("/updateDepartById")
    @ResponseBody
    public String updateDepartById(PtDepartmentEntity ptDepartEntity){
        logger.info(ptDepartEntity);
        departService.updateDepartById(ptDepartEntity);
        return jsonSuccess("depart.update");
    }


    @RequestMapping("/queryOrganListById")
    @ResponseBody
    public PtDepartmentEntity queryOrganListById(@RequestParam(value = "depUuid",required = false) String depUuid){
        logger.info("depUuid"+depUuid);
        PtDepartmentEntity ptDepartEntity = departService.queryDepartListById(depUuid);
        return ptDepartEntity;
    }
    @RequestMapping("/saveBelongCenterIdByDepUuid")
    @ResponseBody
    public String saveBelongCenterIdByDepUuid(@RequestParam("depUuid")String depUuid,@RequestParam("belongCenter")String belongCenter){
        logger.info("depUuid"+depUuid + "belongCenter" + belongCenter);
        departService.saveBelongCenterIdByDepUuid(depUuid,belongCenter);
        return jsonSuccess("depart.bound.parent.success");
    }

    @RequestMapping("/queryDepartInfoListByPage")
    @ResponseBody
    public PadingRstType<PtDepartmentEntity> queryDepartInfoListByPage(SearchJobEntity search, PaddingEntity padding){
        logger.info(search);
        logger.info(padding);
        PadingRstType<PtDepartmentEntity> padingRstType = departService.queryDepartInfoListByPage(search,padding);

        return padingRstType;
    }
    @RequestMapping("/queryUserListByPage")
    @ResponseBody
    public PadingRstType<PtUserEntity> queryUserListByPage(SearchDepartmentEntity entity, PaddingEntity paddingEntity){
        logger.info(entity);
        logger.info(paddingEntity);
        PadingRstType<PtUserEntity>  padingRstType= departService.queryUserInfoListByPage(entity,paddingEntity);
        return padingRstType;
    }

    @RequestMapping("/addDepartInfo")
    @ResponseBody
    public String addDepartInfo(PtDepartmentEntity ptDepartEntity){
        logger.info(ptDepartEntity);
        departService.addDepartInfo(ptDepartEntity);
        return jsonSuccess("depart.add.success");
    }
    @RequestMapping("/queryUserInfoListByPage")
    @ResponseBody
    public PadingRstType<PtUserEntity> queryUserInfoListByPage(SearchDepartmentEntity entity, PaddingEntity paddingEntity){
        logger.info(entity);
        logger.info(paddingEntity);
        PadingRstType<PtUserEntity>  padingRstType= departService.queryUserInfoListByPage(entity,paddingEntity);
        return padingRstType;
    }
    @RequestMapping("/queryDutyListByPage")
    @ResponseBody
    public PadingRstType<PtJobEntity> queryDutyListByPage(SearchDepartmentEntity entity, PaddingEntity paddingEntity){
        logger.info(entity);
        logger.info(paddingEntity);
        PadingRstType<PtJobEntity>  padingRstType= departService.queryDutyListByPage(entity,paddingEntity);
        return padingRstType;
    }
}
